package com.google.jessewilson.codesearch.api;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.jessewilson.codesearch.api.spi.SearchProviderBackend;
import com.google.jessewilson.codesearch.util.Progress;

import java.util.concurrent.ExecutorService;

import ca.odell.glazedlists.EventList;

/**
 * Results from a simple {@link EventList}.
 *
 * @author jessewilson
 */
public class DefaultQueryResponse implements QueryResponse {

  private final Handle handle = new Handle(this);
  private final EventList<Line> lines;
  private final Query query;
  private final Progress progress = new Progress();
  private final ResultsPopulator resultsPopulator;

  public DefaultQueryResponse(ExecutorService executorService,
      EventList<Line> lines, Query query, SearchProviderBackend searchProviderBackend) {
    this.resultsPopulator = new DefaultResultsPopulator(executorService, this,
        searchProviderBackend);
    this.lines = lines;
    this.query = query;
  }

  public EventList<Line> getLines() {
    return lines;
  }

  public String getShortDescription() {
    return query.getSearchTerm();
  }

  public Handle handle() {
    return handle;
  }

  public Query getQuery() {
    return query;
  }

  public void dispose() {
    // do nothing
  }

  public Progress getProgress() {
    return progress;
  }

  public ResultsPopulator getResultsPopulator() {
    return resultsPopulator;
  }

  public static class Factory {
    @Inject private Provider<ExecutorService> executorServiceProvider;
    @Inject private Provider<EventList> eventListProvider;
    public DefaultQueryResponse create(Query query, SearchProviderBackend searchProviderBackend) {
      return new DefaultQueryResponse(executorServiceProvider.get(),
          eventListProvider.get(), query, searchProviderBackend);
    }
  }
}
